home *** CD-ROM | disk | FTP | other *** search
- Path: deneb.dur.ac.uk!d40p5m
- From: Matthew Strawbridge <M.J.Strawbridge@durham.ac.uk>
- Newsgroups: comp.lang.c++
- Subject: GNU question
- Date: Tue, 12 Mar 1996 11:41:32 +0000
- Organization: University of Durham, Durham, UK.
- Message-ID: <Pine.SOL.3.91-941213.960312113456.29919A-100000@deneb.dur.ac.uk>
- NNTP-Posting-Host: deneb.dur.ac.uk
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=US-ASCII
-
-
- Hi,
- I'm new to C++ programming, and rather than try "hello world" I thought
- I'd try to write something a bit more useful! Needless to say, it doesn't
- run, but hopefully some kind soul out there will be able to point out
- where I've gone wrong.
-
- This program is supposed to generate a random number, then read a file of
- one line quotes and save one of them to another file - so that I can
- reference it on my WEB page. I wrote the program with a book on Turbo
- C++, but have to compile it with GNU c++ on unix - hopefully the only
- problem is I've not included the right files, or randomize() and random()
- are differently named (I get 'implicit definition' errors on the lines
- that use these).
-
- Here is the code:
- // quote.cc
- // Generates a random quote from the file quotes.dat
- // Puts the result in quote.of.the.day
-
- // Matthew Strawbridge
- // March '96
-
- #include <stdlib.h> // for rand (), randomize ()
- #include <time.h> // for randomize ()
- #include <fstream.h> // for file streams
-
- void main()
- {
- const int MAX = 80; // size of the buffer
- char buffer[MAX]; // character buffer
- int j, lines, r;
-
- ifstream infile("quotes.dat"); // file for input
- randomize(); // seed random number generator
-
- // Count lines in file
- lines = 0;
- while (!infile.eof() )
- {
- infile.getline(buffer,MAX);
- lines++;
- }
- infile.close();
-
- ifstream infile2("quotes.dat"); // Open the file I just
- closed
- ofstream outfile("quote.of.the.day"); // File for output
- r = random(lines);
-
- for (j = 0; j < r; ++j )
- {
- infile2.getline(buffer,MAX); // Read to random point
- }
-
- outfile << buffer;
-
- }
-
- Many thanks,
- Mat.
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Matthew Strawbridge : M.J.Strawbridge@durham.ac.uk
- WWW : http://www.dur.ac.uk/~d40p5m
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-